package com.guhappy.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import com.guhappy.model.user.Account;
import com.guhappy.model.user.Customer;
import com.guhappy.model.user.CustomerHome;
import com.guhappy.model.user.WebUser;
import com.guhappy.model.user.WebUserHome;
import com.opensymphony.xwork2.ActionSupport;

public class ActivationAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 */
	@Action(value="activateAccount", results={
			@Result(name="success",location="/pages/homePage.jsp"),@Result(name="input", location="/pages/homePage.jsp")
			})
	public String execute() {
		
		HttpServletRequest request = ServletActionContext.getRequest();
		WebUser user = WebUserHome.findById(request.getParameter("id"));
		String checkCode = request.getParameter("checkCode");
		if (null!= user && user.getCodeCheck().equals(checkCode) && !user.getStatus().equals("1")){
			Customer customer = new Customer();
			customer.setWebUser(user);
			user.setStatus("1");
			
			CustomerHome.attachDirty(customer);
			WebUserHome.attachDirty(user);
			request.getSession().setAttribute("webUser", user);
			return SUCCESS;
		}
		else{
			addActionError(getText("user_already_activated"));
			return INPUT;
		}
		// mock start
/*		request.getSession().setAttribute("webUser", user);
		return SUCCESS;*/
		
		// mock end
	}
}
